﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class LoaiSachDAO
    {
    #region 1. Them
        public static bool ThemLoaiSach(LoaiSachDTO loaisach)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiSach", loaisach.MaLoaiSach));
                sqlParams.Add(new SqlParameter("@TenLoaiSach", loaisach.TenLoaiSach));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertLOAISACH", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
    #endregion
    #region 2. Xoa
        public static bool XoaLoaiSachByID(String maloaisach)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiSach", maloaisach));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteLOAISACH", sqlParams);
                if (n == 1)
                    result = true;

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
    #endregion
    #region 3. Sua
        public static bool SuaLoaiSach(LoaiSachDTO loaisach)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiSach", loaisach.MaLoaiSach));
                sqlParams.Add(new SqlParameter("@TenLoaiSach", loaisach.TenLoaiSach));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertLOAISACH", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
    #endregion
    #region 4. Tim kiem
        public static DataTable TimTatCaLoaiSach()
        {
           DataTable dt= new DataTable();
            try
            {
                 dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLOAISACHesAll");
                
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return dt;
        }
        public static List<LoaiSachDTO> TimTatCaLoaiSach1()
        {
            List<LoaiSachDTO> result = new List<LoaiSachDTO>();
            try
            {
                LoaiSachDTO rs= new LoaiSachDTO();
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLOAISACHesAll");
                DataRow dr = dt.Rows[0];
                rs.MaLoaiSach = dr["Mã Loại Sách"].ToString();
                rs.TenLoaiSach = dr["Tên Loại Sách"].ToString();
                result.Add(rs);
            }
            catch (Exception exception)
            {
                
                throw exception;
            }
            return result;
        }
        public static LoaiSachDTO TimLoaiSachByID(String maloaisach)
        {
            LoaiSachDTO result = new LoaiSachDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiSach", maloaisach));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLOAISACH", sqlParams);
                DataRow dr = dt.Rows[0];
                result.MaLoaiSach = dr["MaLoaiSach"].ToString();
                result.TenLoaiSach = dr["TenLoaiSach"].ToString();

            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static bool CheckLoaiSachByID(String maloaisach)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiSach", maloaisach));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLOAISACH", sqlParams);
                if (dt.Rows.Count > 0)
                    result = true;
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
    #endregion
    }
}
